import time
import requests
from lxml import etree
from common.utils import load_yaml


base_url = "http://192.168.10.253:81/redmine/"

conf_data = load_yaml('../config/bug_record.yaml')
print(conf_data)

prj_url = base_url + conf_data['project'] + conf_data['func']
# print(prj_url)


def get_url(url):
    headers = {
        "Cookie": "_redmine_session=VXZWTGZMRmtHZUcvTUN6TjN4aWRPUXpyU0tpVzUvd0txRnRPdC8wMm1CRlpYajBhMmV1dktuR1FxZWYxS05JeE92eUdLQ29hY0E1SU5VeHhvNEFoci9POUZ1SlpIazgzZnR6VWhkazYvOWZROERSR2NTRWltN2hvTnVpcFM1VUpSNGFVUW9LM1BLY3Evc3NPaThMNW1GNkI5TFJWUkdkMGpPSXZ6UGEzUUlqZW1PYjByeE0wN3Bob05DbFhQWXNxVHAxWHo4WXFJR2xiZ3oyU21TWGl0YU9NdlhMdERHSzhDZUJwc1RKQld3OEVhUkR2UTBsY2ErRFN3VUY0YjlnNlRja1U2R3dUSjg0U2Y4NnpZUldsOHo4ZTRia1VJQ1BCbjYxTDlsVm5GRGc1ZTZPODBlOXJtalpkc0hmM3dTUTJsQVIwV3E2UmZTQytRSFRuVC9pczBnNzBpTUhYRzhPTXp6ZUF3R3Q5LzB1a1d2dzZ4OTBkdVFBMzBad3pwbmFwL0hVVUdlUVZwQVZ5VjVQc2t5ZnovRWlTazVHZm4yNUEyWUVXNUl5cGRSR0dBUkd5bVk0eHFxSWlzdnhVaUcwSTJHT2U5SWg5NXJpMUliOW9obkE4ajRlMldpT0s2dFl3OWtPU0wrM3haMmVsck0rV0tlcThKNlZzcUk5cjFJRmZGcVhBd2lFRUplbFpXMHpodnVreUxjUlo3WEpKbkg3ZURzV2RYYitrbGRzPS0tTktMc3A0Q3RPQ3lONmE0cDJJUDdyZz09--6a3ceedf164eec523c396b623001f2b4cd0004dc",

        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chro" \
                      "me/107.0.0.0 Safari/537.36 Edg/107.0.1418.56"
    }

    res = requests.get(url, headers=headers)
    res.encoding = 'utf-8'
    main_html = etree.HTML(res.text)
    table = main_html.xpath('//*[@id="content"]/form[2]/div/table/tbody/tr')
    "/html/body/div[1]/div[2]/div[1]/div[3]/div[2]/form[2]/div/table/tbody/tr"
    for td in table:
        id = td.xpath('./td[2]/a/text()')[0]
        tracker = td.xpath('./td[3]/text()')[0]
        status = td.xpath('./td[4]/text()')[0]
        priority = td.xpath('./td[5]/text()')[0]
        subject = td.xpath('./td[6]/a/text()')[0]
        with open('./data/'+now+'.csv', mode='a', encoding='utf-8') as f:
            f.write(id + ',')
            f.write(tracker + ',')
            f.write(subject + ',')
            f.write(status + ',')
            f.write(priority + ',')
            f.write('\n')
    excel_file = OperateExcel()



if __name__ == '__main__':
    # pass
    # t = datetime.today()
    # # now = t.strftime('%m/%d/%H_%M')
    now = time.strftime('%Y-%m-%d %H_%M_%S')
    # for page in range(1, 5):
    #     prj_url = prj_url.split('?')[0]
    #     prj_url = prj_url+ f"?page={page}"
    #     get_url(prj_url)
    # with open('./data/data1.xlsx') as f:
    #     f.write('a')
    ex = OperateExcel('./data/data1.xlsx')
    for i in range(6):
        for j in range(10):
            ex.write_value(i, j, i+j)
    ex.save_file()